package com.letoo.sso.core.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.letoo.sso.common.domain.ContextConstants;
import com.letoo.sso.common.domain.Result;
import com.letoo.sso.common.domain.ResultCode;
import com.letoo.sso.common.utils.AESUtils;
import com.letoo.sso.common.utils.AssertUtil;
import com.letoo.sso.common.utils.qiniu.QiniuUtils;
import com.letoo.sso.core.service.AdminService;
import com.letoo.sso.core.service.AppBaseConfigService;
import com.letoo.sso.dataaccess.dao.cahce.AppBaseConfigCacheDAO;
import com.letoo.sso.dataaccess.dao.db.AppBaseConfigDBDAO;
import com.letoo.sso.dataaccess.dao.db.BaseDBDAO;
import com.letoo.sso.dataaccess.domain.dbo.AppBaseConfigDO;

/**
 * AppBaseConfigService接口实现
 * 
 * @author: dengjie
 */
@Service("appBaseConfigService")
public class AppBaseConfigServiceImpl extends BaseServiceImpl<AppBaseConfigDO> implements AppBaseConfigService {

  @Resource
  private AppBaseConfigDBDAO appBaseConfigDBDAO;

  @Resource
  private AppBaseConfigCacheDAO appBaseConfigCacheDAO;

  @Resource
  private AdminService adminService;

  @Override
  protected BaseDBDAO<AppBaseConfigDO> getDAO() {
    return appBaseConfigDBDAO;
  }

  @Override
  public Result queryAppConfigByAppId(int appId) {
    AssertUtil.gt(appId, 0, "appTypeId must gt zero");
    List<AppBaseConfigDO> appBaseConfigDOs = appBaseConfigDBDAO.queryAppConfigByAppId(appId);
    AssertUtil.notEmpty(appBaseConfigDOs, "the configs collections is empty");
    Result result = new Result(ResultCode.COMMON_SUCCESS, true);
    // 序列化成JSON后进行对称加密。
    String resContent = JSON.toJSONString(appBaseConfigDOs);
    String baseConfigSecretKey = "45ryu230a@n2x302";
    String resSecretContent = AESUtils.encrypt(resContent, baseConfigSecretKey);
    result.setProperty(ContextConstants.PAGE_ROWS, resSecretContent);

    return result;
  }

  @Override
  public void initAppBaseConfig(int appId) {
    AssertUtil.gt(appId, 0, "appTypeId must gt zero");
    List<AppBaseConfigDO> appBaseConfigDOs = appBaseConfigDBDAO.queryAppConfigByAppId(appId);
    AssertUtil.notEmpty(appBaseConfigDOs, "the configs collections is empty");
    //
    appBaseConfigCacheDAO.setAppBaseConfig(ContextConstants.APP_ID, appBaseConfigDOs);
  }

  @Override
  public String getAppBaseConfig(int appId, String configKey) {
    return appBaseConfigCacheDAO.getAppBaseConfig(appId, configKey);
  }

  @Override
  public Result getUploadToken() {
    // 获取配置里面空间地址。
    String bucketName = getAppBaseConfig(ContextConstants.APP_ID, ContextConstants.PICTURE_STORAGE_BUCKET_NAME);
    // LOG.info("dengjie,bucketName is:" + bucketName);
    String token = QiniuUtils.getUpTokenForSimple(bucketName);
    if (StringUtils.isBlank(token)) {
      Result res = new Result(ResultCode.COMMON_FAILED, false);
      return res;
    }
    Result res = new Result(ResultCode.COMMON_SUCCESS, true);
    res.setProperty("token", token);
    return res;
  }

}
